#include <iostream>
#include <map>
using namespace std;
//关联式容器map的一些删除，查找，添加，遍历
void test(){
    map<int,string> mp={{1,"hello"},{2,"nihao"}};
    //增加键值对
    mp.insert({3,"hi"});
    mp[89]="four";
    mp[100]="handund";
    for(auto&pair:mp){
        cout<<pair.first<<" "<<pair.second<<endl;
    }
    //删除键值对
    mp.erase(100);
    //for(auto&pair:mp){
      //  cout<<pair.first<<" "<<pair.second<<endl;
   // }
    for(auto it=mp.begin();it!=mp.end();++it){
        cout<<it->first<<" "<<it->second<<endl;
    }
    for(auto  pair:mp){
        cout<<pair.first<<" "<<pair.second<<endl;
    }
    //通过下标进行修改，通过迭代器修改
    mp[1]="123";
    mp[1]="456";
    for(auto pair:mp){
        cout<<pair.first<<" "<<pair.second<<endl;
    }

    cout<<"==============="<<endl;
    //使用迭代器进行修改，得先让这个迭代器指向我想要的元素
    auto it=mp.find(3);
    if(it!=mp.end()){
        it->second="third";
        cout<<it->second<<endl;
    }
    
    for(auto pair:mp){
        cout<<pair.first<<" "<<pair.second<<endl;
    }
    
    auto it1=mp.find(89);
    if(mp.count(89)){
        cout<<it1->second<<endl;
    }

    
}
int main(int argc, char * argv[]){
    test();
    return 0;
}

